<?php
// ------------------------------------------------------------------------
// |@Author       : Jarmin <edshop@qq.com>
// |@----------------------------------------------------------------------
// |@Date         : 2023-05-20 22:51:16
// |@----------------------------------------------------------------------
// |@LastEditTime : 2023-05-29 00:06:20
// |@----------------------------------------------------------------------
// |@LastEditors  : Jarmin <jarmin@ladmin.cn>
// |@----------------------------------------------------------------------
// |@Description  : 
// |@----------------------------------------------------------------------
// |@FilePath     : Auth.php
// |@----------------------------------------------------------------------
// |@Copyright (c) 2023 http://www.ladmin.cn   All rights reserved. 
// ------------------------------------------------------------------------
namespace app\api\model;

use think\admin\Model;

class Auth extends Model
{
    public function menus()
    {
        return $this->hasMany(Menu::class, 'id', 'menu_id_list');
    }
    
    /**
     * 获取指定角色的权限列表
     * @param  array $data 查询条件，包含 role_id 字段
     * @return array       菜单 ID 列表
     */
    public static function listAuth(array $data): array
    {
         // 查询指定角色的权限列表和角色信息
         $role = Role::with('auth')->field('id, data_type, dept_list, grid_list, dashboard')->find($data['role_id']);
         // 如果查询角色信息成功，则将dept_list、grid_list和dashboard_list解码
        if ($role) {
            $role['dept_list'] = !empty($role['dept_list']) ? json_decode($role['dept_list']) : [];
            $role['grid_list'] = !empty($role['grid_list']) ? json_decode($role['grid_list']) : [];
        }
        // 组装权限列表和角色信息并返回
        return [
            'auth' => $role['auth'] ? json_decode($role['auth']->menu_id_list) : [],
            'role' => $role ? $role->toArray() : []
        ];
    }
}